app.factory('ApiUtil', ['$http', '$q', function($http, $q) {
    var URL_PREFIX = '';

    var request_method = {
        config: function(config) {
            if (config.hasOwnProperty('URL_PREFIX') && angular.isDefined(config.URL_PREFIX)) {
                URL_PREFIX = config.URL_PREFIX;
            }
        },
        request: function(url, method, params, data, config) {
            var deferred = $q.defer();
            var req_config = config || {};

            if (!/http[s]?:\/\/.*/.test(url)) {
                url = URL_PREFIX + url;
            }
            params = params || {};

            // request data
            $http({
                method: method,
                url: url,
                params: params,
                data: data,
                timeout: req_config.timeout
            }).success(function(data, status, headers, config) {
                deferred.resolve(data);
            }).error(function(data, status, headers, config) {
                deferred.reject(data);
            });

            return deferred.promise;
        },
        post: function(url, params, config) {
            return request_method.request(url, 'POST', null, params, config);
        },
        get: function(url, params, config) {
            return request_method.request(url, 'GET', params, null, config);
        },

        delete: function(url, params, config) {
            return request_method.request(url, 'DELETE', null, params, config);
        },

        patch: function(url, params, config) {
            return request_method.request(url, 'PATCH', null, params, config);
        },
        put: function(url, params, config) {
            return request_method.request(url, 'PUT', null, params, config);
        }
    };

    return request_method;
}]);
app.factory('sweetAlertUtil', ['SweetAlert', function(SweetAlert) {
    return {
        success: function(msg, confirmButtonText, callback) {
            SweetAlert.swal({
                title: msg,
                confirmButtonText: confirmButtonText || '确定',
                confirmButtonColor: '#0ca63b'
            }, function() {
                angular.isFunction(callback) && callback();
            });
        },
        fail: function(msg, text, failCallback, confirmButtonText) {
            SweetAlert.swal({
                title: msg,
                text: text || '',
                confirmButtonColor: "#d43f3a",
                confirmButtonText: confirmButtonText || '确定'
            }, function() {
                angular.isFunction(failCallback) && failCallback();
            });
        },
        confirm: function(title, confirmFun, confirmButtonText, cancelButtonText) {
            SweetAlert.swal({
                title: title,
                showCancelButton: true,
                type: "warning",
                cancelButtonText: cancelButtonText || '取消',
                confirmButtonColor: '#0ca63b',
                confirmButtonText: confirmButtonText || "确定"
            }, function(isConfirm) {
                angular.isFunction(confirmFun) && confirmFun(isConfirm);
            });
        }

    }
}]);

//公共基础接口
app.factory('commomApi', ['sweetAlertUtil', function(sweetAlertUtil) {
    return {
        //请求成功
        // 参数：data-请求成功后返回的数据
        //       callback-返回成功且有效后的回调函数
        // 返回值：无
        requireSuccess: function(data, callback) {
            if (data.errCode !== 0) {
                sweetAlertUtil.fail(data.errMsg);

                return;
            }

            angular.isFunction(callback) && callback();
        },
        //请求失败后错误提示
        // 参数：无
        // 返回值：无
        requireFail: function() {
            sweetAlertUtil.fail('网络出错啦！请稍后再试~~~');
        }

    }
}]);
